package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.client.enums.BalanceLogTypeEnum;
import com.meida.module.pay.client.entity.PayBalance;
import com.meida.module.pay.client.entity.PayCashout;
import com.meida.module.pay.client.entity.PayConfig;
import com.meida.module.pay.client.enums.PayCashoutStatusEnum;
import com.meida.module.pay.client.enums.PayCashoutTypeEnum;
import com.meida.module.pay.client.vo.PayRequest;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.pay.provider.service.PayConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Component
public class UserPayCashoutSaveHandler implements SaveInterceptor<PayCashout> {

    @Autowired
    private PayBalanceService payBalanceService;

    @Autowired
    private PayConfigService payConfigService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        String cashoutType = params.get("cashoutType");
        if (FlymeUtils.isEmpty(cashoutType)) {
            return ResultBody.failed("请选择提现类型");
        }
        BigDecimal money = params.getBigDecimal("money");
        if (FlymeUtils.eqzero(money)) {
            return ResultBody.failed("请输入提现金额");
        }
        Long userId = OpenHelper.getUserId();
        PayBalance payBalance = payBalanceService.addUserBalance(userId);
        BigDecimal balanceAmount = payBalance.getAmount();
        if (FlymeUtils.gt(money, balanceAmount)) {
            return ResultBody.failed("超出可提现金额");
        }
        EntityMap configMap = redisUtils.getConfigMap("CASHOUT_CONFIG");
        BigDecimal minCashoutAmount = configMap.getBigDecimal("minCashoutAmount");
        if (FlymeUtils.gt(minCashoutAmount, money)) {
            return ResultBody.failed("小于最小提现金额");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, PayCashout payCashout) {
        Long userId = OpenHelper.getUserId();
        payCashout.setUserId(userId);
        payCashout.setStatus(PayCashoutStatusEnum.dealWith.getValue());
        EntityMap configMap = redisUtils.getConfigMap("CASHOUT_CONFIG");
        BigDecimal cashoutPoundage = configMap.getBigDecimal("cashoutPoundage");
        //提现手续费比例
        BigDecimal cashoutPoundageProportion = cashoutPoundage.divide(new BigDecimal(100), 2, RoundingMode.HALF_DOWN);
        payCashout.setRatio(cashoutPoundageProportion);
        BigDecimal money = payCashout.getMoney();
        //提现手续费
        BigDecimal commission = money.multiply(cashoutPoundageProportion);
        payCashout.setCommission(commission);
        BigDecimal realMoney = money.subtract(commission);
        payCashout.setRealMoney(realMoney);
        PayBalance payBalance = payBalanceService.addUserBalance(userId);
        String cashoutType = payCashout.getCashoutType();
        PayConfig payConfig = payConfigService.getByProperty("payCode", cashoutType);
        if (cashoutType.equals(PayCashoutTypeEnum.aliPay.getValue())) {
            payCashout.setAliPayAccount(payBalance.getAliPayAccount());
        }
        if (cashoutType.equals(PayCashoutTypeEnum.wxPay.getValue())) {
            payCashout.setWxOpenId(payBalance.getWxOpenId());
        }
        payCashout.setPayId(payConfig.getPayId());
        payCashout.setCheckState(0);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, PayCashout payCashout) {
        String title = "佣金提现";
        Long cashoutId = payCashout.getCashoutId();
        BigDecimal money = payCashout.getMoney();
        String body = title + money;
        PayRequest payRequest = new PayRequest();
        payRequest.setUseBindAmount(false);
        payRequest.setUserId(payCashout.getUserId());
        payRequest.setOutTradeNo(String.valueOf(cashoutId));
        payRequest.setTitle(title);
        payRequest.setBody(body);
        payRequest.setLogType(BalanceLogTypeEnum.logType4.getValue());
        payRequest.setIsCheckPayPwd(false);
        payRequest.setPayAmount(money);
        payRequest.setBusId(cashoutId);
        payRequest.setStatus(0);
        payBalanceService.subtractBalance(payRequest);
    }
}
